<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;

class LoginController extends Controller
{
    public function login()
    {        
    	return view('admin/login/login');
    }

    //dologin
    public function doLogin(Request $request)
    {
        $data = $request -> except('_token');
        // dd($data);

        $this->validate($request, [
            //验证规则，required(不为空)
             'name' => 'required',
            'password' => 'required',
        ],[
             // //错误提醒
             'name.required' => '请输入账号',
             'password.required' => '请输入密码',            
        ]);
        //验证账号
        $name = DB::table('member') -> lists('name');
        if(!in_array($data['name'],$name))
        {
            return back() -> with(['error' => '账户名与密码不匹配，请重新输入']);
        }
               
        //查询密码
        $p = DB::table('member') -> where('name','=',$data['name']) -> first() -> password;
        //验证密码
        if (Hash::check($data['password'], $p)) {
            return redirect('/admin/index') -> with('success','登录成功');
        }else
        {  

            return back() -> with(['error' => '账户名与密码不匹配，请重新输入']);

        }
    }
}
